﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class AssociatedContentMapper
    {
        internal static IAssociatedContent ToBusinessObject ( T_AssociatedContent entity )
        {
            IAssociatedContent obj = new AssociatedContent (
                entity.AssociatedContent_IDPK ,
                entity.AssetID_FK.Value ,
                entity.AssociatedContentTypeID_FK ,
                entity.StatusID_FK ,
                entity.CreationDate_DT ,
                entity.UpdatedDate_DT ,
                entity.ContentValue_NVC
                );

            // parent is causing infinite recursion: obj.Asset = AssetMapper.ToBusinessObject ( entity.T_Asset );
            if ( entity.T_Status != null ) obj.Status = StatusMapper.ToBusinessObject ( entity.T_Status );
            if ( entity.T_AssociatedContentType != null ) obj.AssociatedContentType = AssociatedContentTypeMapper.ToBusinessObject ( entity.T_AssociatedContentType );

            return obj;
        }

        internal static T_AssociatedContent ToEntity ( IAssociatedContent v )
        {
            T_AssociatedContent entity = new T_AssociatedContent ( );

            entity.AssociatedContent_IDPK = v.ID;
            entity.AssetID_FK = v.AssetID;
            entity.AssociatedContentTypeID_FK = v.AssociatedContentTypeID;
            entity.StatusID_FK = v.StatusID;
            entity.CreationDate_DT = v.CreationDate;
            entity.UpdatedDate_DT = v.UpdatedDate;
            entity.ContentValue_NVC = v.ContentValue;

            if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );
            if ( v.AssociatedContentType != null ) entity.T_AssociatedContentType = AssociatedContentTypeMapper.ToEntity ( v.AssociatedContentType );
            // parent is causing infinite recursion: entity.T_Asset = AssetMapper.ToEntity ( v.Asset );

            return entity;
        }

        internal static IList<IAssociatedContent> ToBusinessObjects ( IList<T_AssociatedContent> dalList )
        {
            return dalList.Select ( tmp => ToBusinessObject ( tmp ) ).ToList ( );
        }

        internal static IList<T_AssociatedContent> ToEntities ( IList<IAssociatedContent> entityList )
        {
            EntitySet<T_AssociatedContent> list = new EntitySet<T_AssociatedContent>();
            entityList.Select ( tmp => ToEntity ( tmp ) ).ToList ( ).ForEach(list.Add);
            return list;
        }
    }
}
